package utils;

import org.gdal.gdal.gdal;
import org.gdal.ogr.Driver;
import org.gdal.ogr.ogr;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;

public class SelectFile {
    public static String selectFile() {
        int result = 0;
        File file = null;
        String path = null;
        JFileChooser fileChooser = new JFileChooser();
        FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了，这里重要的一句
        System.out.println(fsv.getHomeDirectory());                //得到桌面路径
        fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
        fileChooser.setDialogTitle("请选择要上传的文件...");
        fileChooser.setApproveButtonText("确定");
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        result = fileChooser.showOpenDialog(null);
        if (JFileChooser.APPROVE_OPTION == result) {
            path=fileChooser.getSelectedFile().getPath();
        }

        return path;
    }

    public static Driver driverInit() {
        // 注册所有的驱动
        ogr.RegisterAll();
        // 为了支持中文路径，请添加下面这句代码
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        // 为了使属性表字段支持中文，请添加下面这句
        gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");
        String strDriverName = "ESRI Shapefile";
        Driver oDriver = ogr.GetDriverByName(strDriverName);
        return oDriver;
    }
}
